package ui;

import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class htmlWindow
{
	String htmlText;
	
    public htmlWindow(String htmlText) {
    	this.htmlText = htmlText;
    }
    

    
    public static String fileToString(String filePath) throws IOException {
    	byte[] buffer = new byte[(int) new File(filePath).length()];
        BufferedInputStream f = null;
        try {
            f = new BufferedInputStream(new FileInputStream(filePath));
            f.read(buffer);
        } finally {
            if (f != null) try { f.close(); } catch (IOException ignored) { }
        }
        return new String(buffer);
    }
    
    public void renderHtml()  {
    	SwingUtilities.invokeLater(new Runnable() {
    		public void run() {
    			JEditorPane jEditorPane = new JEditorPane();
    			jEditorPane.setEditable(false);
    	        JScrollPane scrollPane = new JScrollPane(jEditorPane);
    	        HTMLEditorKit kit = new HTMLEditorKit();
    	        kit.setLinkCursor(new Cursor(Cursor.HAND_CURSOR));
    	        StyleSheet ss = kit.getStyleSheet();
    	        try {
					ss.addRule(fileToString("src/tag/dailymotion.css"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    	        jEditorPane.setEditorKit(kit);
    	        Document doc = kit.createDefaultDocument();
    	        jEditorPane.setDocument(doc);
    	        jEditorPane.setText(htmlText);
    	        
    	        JFrame j = new JFrame("Tag Cloud");
    	        j.getContentPane().add(scrollPane, BorderLayout.CENTER);
    	        j.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	        j.setSize(new Dimension(640,480));
    	        j.setLocationRelativeTo(null);
    	        j.setVisible(true);
    		}
    	});
    }
}